/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ttt.nor2.alarm;

import com.ttt.nor2.db.Ncen;
import com.ttt.nor2.util.ClsUtil;
import com.ttt.nor2.util.ExceptionUtil;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Hashtable;
import java.util.Locale;
import org.apache.log4j.Logger;

/**
 *
 * @author anuphabm
 */
public class ALarmExchange extends MsgExchage {

    private Logger logger = Logger.getLogger(ALarmExchange.class);
    private int class_id;
    private String actionCode;  //1
    private String msgOri;      //2
    private String msgFormat;   //3
    private int alarmNum;    //4
    private String datetime;    //5
    private String date;
    private String time;
    private int alarmId;     //6
    private String type;        //7
    private String urgency;     //8
    private String event;       //9
    private String exchangeName;//10
    private String equName;     //11
    private String equAddr;     //12
    private String alarmText;   //13
    private String urcode;
    private int ncenCode;

    @Override
    @SuppressWarnings("empty-statement")
    public boolean isSavedata() {
        analyzeData();
        String transid = ClsUtil.genRandomID();
        String dateSplit[] = date.split("-");
        int yy = Integer.parseInt(dateSplit[0].trim());
        int mm = Integer.parseInt(dateSplit[1].trim()) - 1;
        int dd = Integer.parseInt(dateSplit[2].trim());
        String timeSplit[] = time.split("H");
        int hh = Integer.parseInt(timeSplit[0].trim());
        int mi = Integer.parseInt(timeSplit[1].trim());
        Locale lc = new Locale("en", "US");
        Calendar c = Calendar.getInstance(lc);
        c.set(2000 + yy, mm, dd, hh, mi, 0);
        String ondate = new SimpleDateFormat("yyyyMMdd", lc).format(c.getTime());
        String dt = new SimpleDateFormat("yyyyMMddHHmmss", lc).format(c.getTime());
        try {
            String sql = "insert into alarm_exchange (transid, class_id, msg_format, alarm_num, datetime_alarm, alarm_id,";
            sql += "type, urgency, event, exchange_name, equ_name, equ_addr, alarm_text, ondate, ur_code, ncen_code)";
            sql += "values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
            PreparedStatement pstmt = cfg.getConn().prepareStatement(sql);
            pstmt.setString(1, transid);
            pstmt.setInt(2, class_id);
            pstmt.setString(3, msgFormat);
            pstmt.setInt(4, alarmNum);
            pstmt.setString(5, dt);
            pstmt.setInt(6, alarmId);
            pstmt.setString(7, type);
            pstmt.setString(8, urgency);
            pstmt.setString(9, event);
            pstmt.setString(10, exchangeName);
            pstmt.setString(11, equName);
            pstmt.setString(12, equAddr);
            pstmt.setString(13, alarmText);
            pstmt.setString(14, ondate);
            pstmt.setString(15, urcode);
            pstmt.setInt(16, ncenCode);
            pstmt.executeUpdate();
            pstmt.close();
            pstmt = null;
            logger.info("dump data[" + transid + "][" + class_id + "][" + msgFormat + "][" + alarmNum + "][" + datetime + "][" + alarmId + "][" + type + "][" + urgency + "][" + event + "][" + exchangeName + "][" + equName + "][" + equAddr + "][" + alarmText + "]done");
        } catch (SQLException e) {
            logger.error("Exception input data error! " + ExceptionUtil.getStackTrace(e));
            System.exit(-1);
            return false;
        }
        return true;
    }

    private void analyzeData() {
        class_id = cfg.getcAlarm().getClassid();
        String dataSplit[] = data.split("/");

        msgFormat = dataSplit[0].trim().replace("@", "");
        alarmNum = Integer.parseInt(dataSplit[1].trim());
        date = dataSplit[2].trim();
        time = dataSplit[3].trim();
        datetime = date + time;
        alarmId = Integer.parseInt(dataSplit[4].split("=")[1].trim());
        type = dataSplit[5].split("=")[1].trim().replace("@", "");
        urgency = dataSplit[6].split("=")[1].trim().replace("@", "");
        event = dataSplit[7].split("=")[1].trim().replace("@", "");
        exchangeName = dataSplit[8].split("=")[1].trim().replace("@", "");
        Hashtable ht = cfg.getHashtable();
        Ncen ncen = (Ncen) ht.get(exchangeName);
        ncenCode = ncen.getNcen_code();
        equName = dataSplit[9].split("=")[1].trim().replace("@", "");
        if (equName.indexOf("-") > -1) {
            urcode = equName.substring(4).replace(" ", "");
            String urSplit[] = urcode.split("-");
            if (urSplit[0].length() == 0) {
                urcode = urSplit[1];
            }
        } else {
            urcode = "0";
        }
        equAddr = dataSplit[10].split("=")[1].trim().replace("@", "");
        alarmText = dataSplit[11].split("=")[1].trim().replace("@", "");
    }
}
